// vite.config.ts
import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import vueJsx from "@vitejs/plugin-vue-jsx";
import qiankun from "vite-plugin-qiankun";
import path from "path";

// https://vitejs.dev/config/
export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd());
  const isQiankunMode = env.VITE_QIANKUN === "true";

  return {
    plugins: [vue(), vueJsx(), qiankun("app1", { useDevMode: !isQiankunMode })],
    resolve: {
      alias: {
        "@": path.resolve(__dirname, "src"),
      },
    },
    server: {
      port: 8081,
      open: true,
      cors: true,
    },
    base: isQiankunMode ? "/app1/" : "/",
    build: {
      rollupOptions: {
        // 当处于 qiankun 模式时，将公共依赖声明为外部依赖
        external: isQiankunMode
          ? [
              "vue",
              "vue-router",
              "pinia",
              "element-plus",
              "axios",
              "@element-plus/icons-vue",
              "@your-org/utils",
            ]
          : [],
        output: {
          globals: isQiankunMode
            ? {
                vue: "Vue",
                "vue-router": "VueRouter",
                pinia: "Pinia",
                "element-plus": "ElementPlus",
                axios: "axios",
                "@element-plus/icons-vue": "ElementPlusIconsVue",
                "@your-org/utils": "YourOrgUtils", // 注意：共享库也需要在这里定义全局变量名
              }
            : {},
        },
      },
    },
  };
});
